package com.dk.academic.controller.base;

import com.github.pagehelper.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;


import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class SuperController {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    protected HttpServletRequest request;
    @Autowired
    protected HttpServletResponse response;
    @Autowired
    protected HttpSession session;
    @Autowired
    protected ServletContext application;

    public SuperController() {
    }
//
//    protected Long getCurrentUserId() {
//        return this.getSSOToken().getId();
//    }
//
//    protected SSOToken getSSOToken() {
//        SSOToken tk = (SSOToken)SSOHelper.attrToken(this.request);
//        if (tk == null) {
//            throw new WebException("-1", "The user does not exist, please relogin.");
//        } else {
//            return tk;
//        }
//    }


    protected <T> Page<T> getPage() {
        return this.getPage(10);
    }

    protected <T> Page<T> getPage(int size) {
        int _size = size;
        int _index = 1;
        if (this.request.getParameter("_size") != null) {
            _size = Integer.parseInt(this.request.getParameter("_size"));
        }

        if (this.request.getParameter("_index") != null) {
            _index = Integer.parseInt(this.request.getParameter("_index"));
        }

        return new Page(_index, _size);
    }

    protected String redirectTo(String url) {
        StringBuffer rto = new StringBuffer("redirect:");
        rto.append(url);
        return rto.toString();
    }

    protected String toJson(Object object) {
        return JSON.toJSONString(object, new SerializerFeature[]{SerializerFeature.BrowserCompatible});
    }

    protected String toJson(Object object, String format) {
        return format == null ? this.toJson(object) : JSON.toJSONStringWithDateFormat(object, format, new SerializerFeature[]{SerializerFeature.WriteDateUseDateFormat});
    }

}